package com.qf.jie.controller;

import com.qf.jie.controller.servlet.BaseServlet;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.CommodityService;
import com.qf.jie.service.impl.CommodityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Date;

@WebServlet("/CommodityController")
@MultipartConfig
public class CommodityController extends BaseServlet {
    CommodityService commodityService = new CommodityServiceImpl();
    // 1.添加商品
    protected RespBean addCommodity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String stock = req.getParameter("stock");
        String price = req.getParameter("price");
        Part file_img = req.getPart("file_img");
        return commodityService.addCommodity(req, file_img, name, stock, price);
    }

    // 2.所有商品分页展示
    protected RespBean getCommodityPageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String len = req.getParameter("len");
        return commodityService.getPage(currentPage, len);
    }

    // 3.用户(卖家)获取自己店铺的商品信息
    protected RespBean getCommodityListByUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return commodityService.getCommodityListByUserId(req);
    }

    // 4.删除自己的一个商品
    protected RespBean deleteCommodityById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        return commodityService.deleteCommodityById(req, id);
    }

    // 5.修改商品信息
    //(HttpServletRequest req, Part part, String strId, String name, String strStock, String strPrice)
    protected RespBean updateCommodityById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String stock = req.getParameter("stock");
        String price = req.getParameter("price");

        return commodityService.updateCommodityById(req, id, name, stock, price);
    }
    // 6.更改图片
    protected RespBean updateCommodityImgById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Part file_img = req.getPart("file_img");

        return commodityService.updateCommodityImgById(req,file_img, id);
    }

    //7.商品详细信息（显示评论）
    protected RespBean CommodityDetailed(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commodity_id = req.getParameter("commodity_id");
        return commodityService.CommodityDetailed(commodity_id);
    }

    //8.添加评论
    protected RespBean addComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String commodity_id = req.getParameter("commodity_id");
        String text = req.getParameter("text");
        return commodityService.addComment(req, commodity_id, text);
    }

}
